package com.qbm.association.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.overzealous.remark.Remark;
import com.qbm.association.bean.ResultBean;
import com.qbm.association.config.Audience;
import com.qbm.association.entity.Article;
import com.qbm.association.entity.Club;
import com.qbm.association.mapper.ArticleMapper;
import com.qbm.association.mapper.ClubMapper;
import com.qbm.association.service.ArticleService;
import io.github.furstenheim.CopyDown;
import io.github.furstenheim.Options;
import io.github.furstenheim.OptionsBuilder;
import io.jsonwebtoken.lang.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Aron
 * @since 2022-02-24
 */
@RestController
@RequestMapping("/association/article")
public class ArticleController {

    @Autowired
    private Audience audience;

    @Resource
    private ArticleMapper articleMapper;

    @Resource
    private ClubMapper clubMapper;

    @Resource
    private ArticleService articleService;


    @GetMapping("getArticlePageList")
    public ResultBean getArticlePageList(Integer clubId, Integer status, Integer currentPage, String title) {
        QueryWrapper<Article> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("club_id", clubId);
        if (status != -1) {
            queryWrapper.eq("status", status);
        }
        if (title != null && title.length() != 0) {
            queryWrapper.like("title", title);
        }
        Page<Article> page = new Page<>(currentPage, 5);
        Page<Article> pageList = articleMapper.selectPage(page, queryWrapper);
        String clubName = clubMapper.selectOne(new QueryWrapper<Club>().eq("id", clubId)).getName();
        Map<String, Object> map = new HashMap<>();
        map.put("pageList", pageList);
        map.put("clubName", clubName);
        return ResultBean.success(map, "获取成功");
    }

    @GetMapping("getArticlePageListWithoutHTML")
    public ResultBean getArticlePageListWithoutHTML(Integer clubId, Integer status, Integer currentPage, String title) {
        QueryWrapper<Article> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("club_id", clubId);
        if (status != -1) {
            queryWrapper.eq("status", status);
        }
        if (title != null && title.length() != 0) {
            queryWrapper.like("title", title);
        }
        Page<Article> page = new Page<>(currentPage, 5);
        Page<Article> pageList = articleMapper.selectPage(page, queryWrapper);

        List<Article> articleList = pageList.getRecords();
//        去除HTML的操作
//        Remark remark = new Remark();
//        for (int i = 0; i < articleList.size(); i++) {
//            articleList.get(i).setContent(remark.convert(articleList.get(i).getContent()));
//        }
        pageList.setRecords(articleList);

        String clubName = clubMapper.selectOne(new QueryWrapper<Club>().eq("id", clubId)).getName();
        Map<String, Object> map = new HashMap<>();
        map.put("pageList", pageList);
        map.put("clubName", clubName);
        return ResultBean.success(map, "获取成功");
    }

    @GetMapping("getArticle")
    public ResultBean getArticle(Integer articleId) {
        QueryWrapper<Article> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", articleId);
        Article article = articleMapper.selectOne(queryWrapper);
        Remark remark = new Remark();
        String contentText = remark.convert(article.getContent());
        article.setContent(contentText);
//        String result = article.getContent();
//        OptionsBuilder optionsBuilder = OptionsBuilder.anOptions();
//        Options options = optionsBuilder.withBr("-")
//                // more options
//                .build();
//        CopyDown converter = new CopyDown(options);
//        String markdown = converter.convert(result);
//        System.out.println(markdown);
//        article.setContent(markdown);
        return ResultBean.success(article);
    }

    @PostMapping("deleteArticle")
    public ResultBean deleteArticle(Article article) {
        boolean isTrue = articleService.removeById(article);
        Assert.isTrue(isTrue, "删除失败");
        return ResultBean.success("删除成功");
    }

    @PostMapping("updateArticle")
    public ResultBean updateArticle(Article article) {
        boolean isTrue = articleService.updateById(article);
        Assert.isTrue(isTrue, "更新失败");
        return ResultBean.success("更新成功");
    }

    @PostMapping("addArticle")
    public ResultBean addArticle(Article article) {

        boolean isTrue = articleMapper.insert(article) == 1;
        Assert.isTrue(isTrue, "添加失败");
        return ResultBean.success("添加成功");
    }

    @GetMapping("getPageArticleByTitle")
    public ResultBean getPageArticleByTitle(Integer currentPage, String title) {
        QueryWrapper<Article> queryWrapper = new QueryWrapper<>();
        if (title != null && title.length() != 0) {
            queryWrapper.like("title", title);
        }
        Page<Article> page = new Page<>(currentPage, 5);
        Page<Article> page1 = articleMapper.selectPage(page, queryWrapper);
        return ResultBean.success(page1, "获取成功");
    }


}

